<?php
class IndexAction extends Action{
	public $cache_time = 3600;
	public function _initialize() {
			//初始化用户相关常量，例如用户购物车信息，用户地区信息（通过地区显示相关产品），最新礼物等
			//这些操作是否应放在，Action中完成？
	}

	
	public function newindex1() {
		
		$pg_index_data = S('pg_index_data');//首页缓存数据 
		
		/**
		$filename = '_pg'.APP_NAME.'_'.MODULE_NAME.'_'.ACTION_NAME;
		if(file_exists(DATA_PATH.$filename.'.php')){
			$pg_index_data	=	F($filename);
		}
		**/

		
		if(!$pg_index_data){
			//首页相关设置数据
			$hpopt = model('Xdata')->lget('hpopt');
			$hpopt['top_imgs'] = unserialize($hpopt['top_imgs']);
			$hpopt['recommend'] = unserialize($hpopt['recommend']);
			$pg_index_data['hpopt']=$hpopt;
			
			$docdata = service('Common')->selectBulletin(6);
			$pg_index_data['docdata']=$docdata;
			
			//品牌中心 10个
			/**
			$brandList = service('Brand')->selectShowBrand(10);
			$pg_index_data['brandList']=$brandList;
			**/
			
			//热销商品 10个
			$hot_where = null;
			if(!empty($hpopt['hot']))$hot_where['shelf_id'] = array( 'in' , explode(',', $hpopt['hot']) );
			$hotProduct = service('Product')->selectByLimit($hot_where,'shelf_send_number desc',8);
			$pg_index_data['hotProduct']=$hotProduct;
			
			//上架新品 5个
			$newProduct = service('Product')->selectByLimit(null,'shelf_on_time desc',4);
			$pg_index_data['newProduct']=$newProduct;
			
			//dump($pg_index_data);
			
			S('pg_index_data',$pg_index_data,3600);   
			//F($filename,$pg_index_data);
			//echo "add to cache;";
		}
		
		
		//$tag = $_REQUEST['tag'];
		
		
			
		$partner_dis = getSubByKey($hotProduct,'partner_id');
			
		
		
		$this->assign($pg_index_data);
	
		/** 彩信模板暂时注销掉
		$where['is_delete']='N';
		$where['is_uploadbyuser']='N';
		$cards = M('SmsTemplet')->where($where)->limit(10)->order('ctime desc')->findAll();
		$this->assign('cards',$cards);
		**/
		
		//加载需要的数据 
		$this->display();
	}
	
	
	public function index() {
		
		$pg_index_data = S('pg_index_data');//首页缓存数据 
		$pg_index_data_tags = S('pg_index_data_tags');//首页缓存数据
		$pg_index_data_tag_p = S('pg_index_data_tag_p');//首页缓存数据 
		$pg_index_data_del_p = S('pg_index_data_del_p');//首页缓存数据 
		//$pg_index_data = null;
		//依据热门地点查询相关信息，以此为：对应商户信息，对应商品信息。
		$tag = $_REQUEST['tag'];
		
		//热门地点
		if(!$pg_index_data_tags){
			//echo "<br>tags_cache is empty run db query!";
			$tags = model('AreaPopular')->getList($city_id,$area_id,10);
			$pg_index_data_tags = $tags;
			S('pg_index_data_tags',$tags,$this->cache_time);
		}
		if(empty($tag)){
			$tag = $pg_index_data_tags[0]['title'];
		}
		
		//首页相关设置数据
		if(!$pg_index_data){
			//echo "<br>index_data_cache is empty run db query!";
			$hpopt = model('Xdata')->lget('hpopt');
			$hpopt['top_imgs'] = unserialize($hpopt['top_imgs']);
			$hpopt['recommend'] = unserialize($hpopt['recommend']);
			$pg_index_data['hpopt']=$hpopt;
			
			//公告数据
			$docdata = service('Common')->selectBulletin(6);
			$pg_index_data['docdata']=$docdata;
			
			S('pg_index_data',$pg_index_data,$this->cache_time);   
		}
		
		$tags_tmp = getSubByKey($pg_index_data_tags,'title');
		//dump($tags_tmp);
		//echo $tag." - ".in_array($tag,$tags_tmp);
		if( in_array($tag,$tags_tmp) && (!$pg_index_data_tag_p||empty($pg_index_data_tag_p[$tag])) ){
			
			$partners = service('Brand')->selectBSubsByTag( $tag ,null, null );
			//echo "<br>tag_product_cache is empty run db query! tag=".$tag;
			$ary_bids = getSubByKey($partners,'parent_id');
			$ary_bids = array_unique($ary_bids);
			//查询地点对应下品牌对应的商品
			//dump($ary_bids);
			$hotProduct = service('Product')->select4FirstPageProduct($ary_bids,'self',10);
			
			$pg_index_data_tag_p[$tag]['partners']=$partners;
			$pg_index_data_tag_p[$tag]['hotProduct']=$hotProduct;
			S('pg_index_data_tag_p',$pg_index_data_tag_p,$this->cache_time);   
		}
		if(!$pg_index_data_del_p){
			$ary_bids = service('Product')->selectBrandId4User(array('get_type'=>'delivery'),30);
			$ary_bids = getSubByKey($ary_bids,'partner_id');
			$ary_bids = array_unique($ary_bids);
			$delProduct = service('Product')->select4FirstPageProduct($ary_bids,'delivery',10);
			$pg_index_data_del_p=$delProduct;
			S('pg_index_data_del_p',$pg_index_data_del_p,$this->cache_time); 
			//dump($pg_index_data_del_p); 
		}
			
		$this->assign($pg_index_data_tag_p[$tag]);
		$this->assign('tag',$tag);
		$this->assign('tags',$pg_index_data_tags);
		$this->assign('delProduct',$pg_index_data_del_p);
		$this->assign($pg_index_data);
	
		//加载需要的数据 
		$this->display('new_index1');
	}
	
	public function newindex() {
		
		//$pg_index_data = S('pg_index_data');//首页缓存数据 
		
		if(!$pg_index_data){
			//首页相关设置数据
			$hpopt = model('Xdata')->lget('hpopt');
			$hpopt['top_imgs'] = unserialize($hpopt['top_imgs']);
			$hpopt['recommend'] = unserialize($hpopt['recommend']);
			$pg_index_data['hpopt']=$hpopt;
			
			$docdata = service('Common')->selectBulletin(4);
			$pg_index_data['docdata']=$docdata;
			
			//品牌中心 10个
			$brandList = service('Brand')->selectShowBrand(10);
			$pg_index_data['brandList']=$brandList;
			
			
			//热销商品 10个
			/**
			$hot_where = null;
			if(!empty($hpopt['hot']))$hot_where['shelf_id'] = array( 'in' , explode(',', $hpopt['hot']) );
			$hotProduct = service('Product')->selectByLimit($hot_where,'shelf_send_number desc',8);
			$pg_index_data['hotProduct']=$hotProduct;
			
			//上架新品 5个
			$newProduct = service('Product')->selectByLimit(null,'shelf_on_time desc',4);
			$pg_index_data['newProduct']=$newProduct;
			**/
			S('pg_index_data',$pg_index_data,3*3600);   
			//F($filename,$pg_index_data);
			//echo "add to cache;";
		}
		
		$this->assign($pg_index_data);
	
		/** 彩信模板暂时注销掉
		$where['is_delete']='N';
		$where['is_uploadbyuser']='N';
		$cards = M('SmsTemplet')->where($where)->limit(10)->order('ctime desc')->findAll();
		$this->assign('cards',$cards);
		**/
		
		//加载需要的数据 
		$this->display("new_index");
	}
	
	
	
	public function help(){
		$docid = $_GET['docid'];
		//if(empty($docid))$this->error('请求参数有误！');
		$helpdoc = service('Common')->getDocById($docid);
		$this->assign('helpdoc', $helpdoc);
		
		$helpdocdata = service('Common')->selectFootDoc();
		$this->assign('helpdocdata', $helpdocdata);
		$this->display();
	}
	
	
	public function bulletin(){
		$docid = $_GET['docid'];
		//if(empty($docid))$this->error('请求参数有误！');
		$bulldoc = service('Common')->getBulletinById($docid);
		$this->assign('bulldoc', $bulldoc);
		
		$docdata = service('Common')->selectBulletin();
		$this->assign('docdata', $docdata);
		$this->display();
	}
	
	public function agreement(){
		$sortcode = $_GET['sortcode'];
		$docdata = service('Common')->getDocBySortCode($sortcode);
		$this->assign('docdata', $docdata);
		$this->display();
	}
	
	
	public function space(){
		$suid = $_GET['suid'];
		if(empty($suid))$this->error('用户不存在！');
		$friend = model('User')->getById($suid);
		if(empty($friend))$this->error('用户不存在！');
		
		$collectpage = service('Collect')->search4User(Dict::DESIROUS,$suid,12);//

		$this->assign('collectpage', $collectpage);
		$this->assign('friend', $friend);
		$this->display();
	}
	
	
	public function advisoryList(){
		$sortcode = h($_REQUEST['sortcode']);
		if(empty($sortcode))$sortcode='suggest';
		$addata = service('Common')->searchAdvisory4User($sortcode,null,2);
		$this->assign('addata', $addata);
		$this->assign('sortcode', $sortcode);
		$this->display();
	}
	
	/**
	 * 通过券码、手机号、验证码查看对应的券信息。
	 */
	public function searchcoupon(){
		
		$passcode = $_POST['passcode'];
		$mobile = $_POST['mobile'];
		if(empty($passcode)){echo "passcode";exit;}
		if(!isValidMobile($mobile)){echo "mobile";exit;}
		//检查验证码
		if(empty($_POST['verify']) || md5($_POST['verify'])!=$_SESSION['verify']){echo "verify";exit;}
		$_SESSION['verify']=time();//查询成功后，重新设置验证码。
		
		$coupon = service('Coupon')->getByPasscode($passcode);
		if(empty($coupon)||$coupon['receiver_mobile']!=$mobile){
			echo "coupon";exit;
		}
		
		$this->assign('coupon', $coupon);
		$this->display();
	}
	
	
	/**
     +----------------------------------------------------------
     * 用户、商户  咨询，报错，问答
     +----------------------------------------------------------
     * @access protected
     +----------------------------------------------------------
     * @param string $message 提示信息
     * @param Boolean $ajax 是否为Ajax方式
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
    public function advisory(){
    	$this->gotoLogin();
    	$dialogueData['surl']=U('Index://Index/submitadvisory');//提交url
    	$dialogueData['advisorytype']='suggest';//咨询分类 suggest error question
    	$dialogueData['showtype']=array('suggest','error','question');//咨询分类 suggest error question 在tab中显示哪些
    	$this->assign('dialogueData', $dialogueData);
    	$this->display(SITE_PATH.'/Public/advisory.html');
    }
    
	public function submitadvisory(){
		$this->gotoLogin();
		$qtext = h($_POST['qtext']);
		$opttype = h($_POST['opttype']);
		if( strlen($qtext) > 110 ){
			exit(json_encode(array('status'=>-1,'msg'=>'内容长度需小于100字符！')));
		}
		if( !in_array($opttype,array('suggest','error','question') ) ){
			exit(json_encode(array('status'=>-1,'msg'=>'请选择类型')));
		}
		$id = service('Common')->submitDialogue($_SESSION['mid'],'user',$opttype,$qtext);
		if($id){
			exit(json_encode(array('status'=>1,'msg'=>'提交成功！将尽快处理！')));
		}else{
			exit(json_encode(array('status'=>0,'msg'=>'提交失败！')));
		}
	}
	
	
}
